var ajaxModule = function(){
    
    var servlet = "budget";
    var tagList = [];
    var bankList = [];
    return { 
        
        
        // Hämtar de senaste transactionerna
        getLatestTransactions: function() {

            $.ajax({
                type: "GET",
                url: servlet + "?action=getLatestTransactions",
                dataType: "xml",
                success: function(xml) {

                    var data = "";
                    var id;
                    var date;
                    var title;
                    var bank;
                    var tags;


                    $(xml).find('transaction').each(function() {

                       id       = $(this).find("id").text();
                       date     = $(this).find("date").text();
                       diff    = $(this).find("diff").text();
                       title    = $(this).find("title").text();
                       bank     = $(this).find("bank").text();
                       tags     = "Mat";

                       data += "<tr><td>" + date + "</td><td>" + diff + "</td><td>" + title + "</td><td>" + tags + "</td><td>" + bank + "</td></tr>\n";

                    });    

                    layoutModule.shrinkMenu();
                    layoutModule.loadContent("#latest_transactions", data);

                }
            });
        },

        // Hämtar de senaste taggarna
        getLatestTags: function() {
            
            var data = "";
            var row;
        
            if(tagList.length < 1) {
               $.ajax({
                    type: "GET",
                    url: "resources/xml/tags.xml",
                    dataType: "xml",
                    success: function(xml) {

                        
                        var id;
                        var name;

                        $(xml).find('tag').each(function() {

                           id       = $(this).find("id").text();
                           name     = $(this).find("name").text();

                           row = "<li id='" + id + "'>" + name + "</li>\n";
                           data += row;
                           tagList.push(row);        
                           
                        });
                        layoutModule.loadContent("#latest_tags", data);
                    }
                });
            
            }
            else {
                for(row in tagList)
                    data += tagList[row];
            layoutModule.loadContent("#latest_tags", data);
            }
        
        },
        
        // Sparar transaction
        saveTransaction: function() {

            data = $('#form_add_transaction').serialize();


            $.ajax({
                type: "POST",
                url: servlet + "?action=addTransaction",
                data: data,
                success: function(res) {

                    if(eval(res.toString(), "true")) {
                        layoutModule.hidePopup();
                        ajaxModule.getLatestTransactions();
                    }
                    else {
                        layoutModule.showFormError(res);
                    }

                }
            });



            return false;
        },


        getSettings: function () {
          
            var fName;        
            var lName;
            var username;
            var email;

          
          $.ajax({
                type: "GET",
                url: servlet + "?action=getSettings",
                dataType: "xml",
                success: function(xml) {


                    fName       = $(xml).find("fName").text();
                    lName     = $(xml).find("lName").text();
                    username    = $(xml).find("username").text();
                    email     = $(xml).find("email").text();
                    
                    
                    $.ajax({
                        type: "GET",
                        url: "resources/snippets/settings.xhtml",
                        dataType: "html",
                        success: function(data) {
                    
                        layoutModule.showPopup(data);
                       
                       
                        $('#popup_area').find('input[name=fName]').val(fName);
                        $('#popup_area').find('input[name=lName]').val(lName);
                        $('#popup_area h2').append(username);
                        $('#popup_area').find('input[name=email]').val(email);

                            
                    }});

                }
            });
           
        },
        
        saveUser: function() {
            
            inputs = $('#form_settings_user input').serialize();
            $.ajax({
                type: "POST",
                url: servlet + "?action=saveUser",
                data: inputs,
                success: function(res) {

                    if(eval(res.toString(), "true")) {
                        layoutModule.hidePopup();
                    }
                    else {
                        layoutModule.showFormError(res);
                    }

                }
            });
            
            
            
            
        },


        getCategories: function() {

            $.get(servlet + '?action=getCategories', function(latest) {

                layoutModule.shrinkMenu();
                layoutModule.loadContent(latest);

            });

        },


        getPopup: function() {

            $.get('resources/snippets/settings.xhtml', function(latest) {

                $('')

            });

        },
        
        
        getBanks: function() {
            
          var names = "";
            
          $.ajax({
                type: "GET",
                url: servlet + "?action=getBanks",
                dataType: "xml",
                success: function(xml) {
                    
                    $(xml).find('banks').each(function() {
                       
                        $(this).find('bank').each(function() {
                            name       = $(this).find("name").text();
                            names = "<li>" + name + "</li>";
                        });
                
            
        
                    });
                    
                    alert(names);
                    
                    $.ajax({
                        type: "GET",
                        url: "resources/snippets/banks.xhtml",
                        dataType: "html",
                        success: function(data) {
                    
                        layoutModule.showPopup(data);
                        $("#popup_area ul").html(names);
                            
                    }});

                }
            });
          
            
        },
        
        logout: function() {
            
            $.get('login?action=logout', function() {
               
               layoutModule.toneRedirect('login.xhtml');
               
            });
            
        }

    }

}();